package com.yilin.medical.lsh;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import com.yilin.medical.app.JxApplication;

import android.os.Bundle;
import android.util.Log;

/**
 * 网络请求与解析
 * 
 * @author jian
 * 
 */
public class NetHelper {

	// public static final String exception_message_timeout = "";
	// public static final String exception_message_data = "服务器异常";
	//
	// public static final String isnot_have_internet ="";

	// 是否联网网络
	public static boolean IsHaveInternet() {
		return !JxApplication.IsHaveInternet();
	}

	/**
	 * get
	 * 
	 * @param url
	 * @param params
	 * @param c
	 * @return
	 * @throws SimpleException
	 */
	public static <T> T httpGet(String url, Map<String, Object> params,
			Class<T> c) throws SimpleException {

		String result = httpGet(url, params);
		Log.i("HTTP", result);
		if (StringUtils.isEmpty(result)) {
			try {
				return c.newInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		T fromJson = JsonUtil.jsonToBean(result, c);
		return fromJson;
	}

	public static <T> T httpGet(String url, Map<String, Object> params, Type t)
			throws SimpleException {

		String result = httpGet(url, params);
		Logs.i("HTTP", result);
		if (StringUtils.isEmpty(result)) {
			throw SimpleException.newInstance("fuwuqiyichang");
		}
		@SuppressWarnings("unchecked")
		T fromJson = (T) JsonUtil.jsonToBean(result, t);
		return fromJson;
	}

	/**
	 * get
	 * 
	 * @param <V>
	 * @param url
	 * @param params
	 * @param c
	 * @return
	 * @throws SimpleException
	 */
	public static <T> T httpGet(String url, Bundle params, Class<T> c)
			throws SimpleException {
		Map<String, Object> map = new HashMap<String, Object>();
		if (params != null) {
			for (String s : params.keySet()) {
				map.put(s, params.get(s));
			}
		}
		return httpGet(url, map, c);
	}

	public static String httpGet(String url, Map<String, Object> params)
			throws SimpleException {
		if (IsHaveInternet()) {
			throw SimpleException.newInstance("wuwangluo");
		}
		return MyHttpClient.requestGetHttp(url, params);
	}

	public static String httpGet(String url) throws SimpleException {
		if (IsHaveInternet()) {
			throw SimpleException.newInstance("wuwangluo");
		}
		return MyHttpClient.requestGetHttp(url);
	}

	/**
	 * post 请求 返回字符串
	 * 
	 * @param url
	 * @param params
	 * @return
	 * @throws SimpleException
	 */
	public static String httpPost(String url, Map<String, Object> params)
			throws SimpleException {
		System.out.println(url + params.toString());
		if (IsHaveInternet()) {
			throw SimpleException.newInstance("wuwangluo");
		}
		return MyHttpClient.requestPostHttp(url, params);
	}

	/**
	 * post 请求 返回对象
	 * 
	 * @param url
	 * @param params
	 * @param c
	 * @return
	 * @throws SimpleException
	 */
	public static <T> T httpPost(String url, Map<String, Object> params,
			Class<T> c) throws SimpleException {
		String result = httpPost(url, params);
		if (StringUtils.isEmpty(result)) {
			try {
				return c.newInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		T fromJson = JsonUtil.jsonToBean(result, c);
		return fromJson;
	}

	@SuppressWarnings("unchecked")
	public static <T> T httpPost(String url, Map<String, Object> params, Type t)
			throws SimpleException {

		String result = httpPost(url, params);
		Logs.i("HTTP", result);
		T fromJson = (T) JsonUtil.jsonToBean(result, t);
		return fromJson;
	}

}